<?php
/**
 * Created by JetBrains PhpStorm.
 * User: CAIXUDONG
 * Date: 11-6-16
 * Time: 下午4:06
 */
 
include '../bootstrap.php';

$_user = Model::getInstance('user');
$user_info = $_user->needLogin();

$cate_name = $_input->post('cate_name');

if(!$cate_name){
    $data = array(
        "statusCode"=>"300",
        "message"=>"必须填写分类名",
    );
    echo Response::JSON($data);
    exit;
}

$action = $_input->post('action');
$parent_cate_id = $_input->post('parent_cate_id',0);
$role_ids = $_input->post('role_ids',false);
$is_show = $_input->post('is_show');
$cate_id = $_input->post('cate_id');

$ret = Model::getInstance('cate')->updateCate($cate_id,$cate_name,$action,$parent_cate_id,$is_show);

if($ret){

    if($role_ids){
        $role_ids = explode(',',$role_ids);
        $role_ids = array_unique($role_ids);
        Model::getInstance('catehasrole')->delByCateId($cate_id);
        foreach($role_ids as $v){
            if($v){
                Model::getInstance('catehasrole')->add($cate_id,$v);
            }
        }
    }
    $data = array(
        "statusCode"=>"200",
        "message"=>"更新成功",
        "navTabId"=>"cate",
    );
    echo Response::JSON($data);
}else{

    $data = array(
        "statusCode"=>"300",
        "message"=>"更新失败",
    );
    echo Response::JSON($data);
}